﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhotoMgr.src.common;
using PhotoMgr.src.database;
using popLib;

namespace PhotoMgr.src.gui.photo
{
    public partial class ContestForm : Form
    {
        List<Contest_GetListContestByAdvisor_Result> contest;

        public int SelectedContestID = -1;
        
        public ContestForm()
        {
            InitializeComponent();

            this.Icon = PhotoMgr.Properties.Resources.logo32;

            Login loginForm = new Login();
            loginForm.ShowDialog();
            if (loginForm.DialogResult != System.Windows.Forms.DialogResult.OK)
            {
                this.Opacity = 0;
                FormLib.ExitTimer(this, 100);
                return;
            }

            dataGridView1.AutoGenerateColumns = false;
            Global.InitDataGridView(dataGridView1, new DataGridViewColumn[] { 
                Global.GenColumn("Tên cuộc thi", "ContestName").SetSizeMode(DataGridViewAutoSizeColumnMode.Fill),
                Global.GenColumn("Ngày bắt đầu", "DateStart").SetFormat("dd/MM/yyyy"),
                Global.GenColumn("Ngày kết thúc", "DateEnd").SetFormat("dd/MM/yyyy"),
                Global.GenColumn("Vòng thi đấu", "ActiveRoundLevel"),
                Global.GenColumn("Tình trạng", "StatusName"),
                Global.GenColumn("#", "ContestID").SetVisible(false),
                Global.GenColumn("#", "Status").SetVisible(false),
            });

            contest = new List<Contest_GetListContestByAdvisor_Result>();
            ReloadData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null) return;
            SelectedContestID = NumberLib.getInt(dataGridView1.CurrentRow.Cells[5].Value);
            string status = dataGridView1.CurrentRow.Cells[6].Value.ToString();

            if (status == "F")
            {
                //FormLib.alert("Cuộc thi này đã kết thúc!!\r\nXin vui lòng chọn cuộc thi khác");
                new ContestResultForm(SelectedContestID).ShowDialog();
                return;
            }

            this.Hide();
            new HostForm2(SelectedContestID, ScoreType.MainScore).ShowDialog();
            this.Show();
            ReloadData();
        }

        void ReloadData()
        {
            contest = DBManager.Contest_GetListContestByAdvisor(UserAccount.Instance.UserID);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = contest;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            button1_Click(null, new EventArgs());
        }
    }
}
